package dao;

import beans.AnimalNaoAdulto;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AnimalNaoAdultoDAO {

    PreparedStatement pst;
    ResultSet rs;

    public AnimalNaoAdultoDAO() {
    }

    public AnimalNaoAdulto load(int mes, int ano, int numBrinco) {
        AnimalNaoAdulto nadlt = new AnimalNaoAdulto();

        String query = "SELECT * FROM naoadulto NATURAL JOIN animal WHERE ano = ? AND mes = ? AND numBrinco = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ano);
            pst.setInt(2, mes);
            pst.setInt(3, numBrinco);

            rs = pst.executeQuery();

            if (rs.next()) {
                nadlt.setAno(rs.getInt("ano"));
                nadlt.setMes(rs.getInt("mes"));
                nadlt.setNumBrinco(rs.getInt("num_brinco"));
                nadlt.setRaca(rs.getString("raca"));
                nadlt.setPeso(rs.getFloat("peso"));
				nadlt.setBezerro(rs.getBoolean("bezerro"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        return nadlt;
    }

    public ArrayList<AnimalNaoAdulto> loadRebanho(int mes, int ano) {
        ArrayList<AnimalNaoAdulto> naoadultos = new ArrayList<AnimalNaoAdulto>();

        String query = "SELECT * FROM naoadulto NATURAL JOIN animal WHERE ano = ? AND mes = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ano);
            pst.setInt(2, mes);
            rs = pst.executeQuery();
            
            while (rs.next()) {
                AnimalNaoAdulto nadlt = new AnimalNaoAdulto();

                nadlt.setAno(rs.getInt("ano"));
                nadlt.setMes(rs.getInt("mes"));
                nadlt.setNumBrinco(rs.getInt("num_brinco"));
                nadlt.setRaca(rs.getString("raca"));
                nadlt.setPeso(rs.getFloat("peso"));
				nadlt.setBezerro(rs.getBoolean("bezerro"));

                naoadultos.add(nadlt);
            }
        } catch (SQLException ex) {
            ex.getLocalizedMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return naoadultos;
    }

    public void insert(AnimalNaoAdulto nadlt) {
        String query = "BEGIN;";
        query += "INSERT INTO animal (ano, mes, numBrinco) VALUES (?, ?, ?, ?);";
        query += "INSERT INTO naoadulto (ano, mes, numBrinco, raca, peso, bezerro) VALUES(?, ?, ?, ?, ?, ?);";
        query += "COMMIT;";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, nadlt.getAno());
            pst.setInt(2, nadlt.getMes());
            pst.setInt(3, nadlt.getNumBrinco());
            pst.setInt(4, nadlt.getAno());
            pst.setInt(5, nadlt.getMes());
            pst.setInt(6, nadlt.getNumBrinco());
            pst.setString(7, nadlt.getRaca());
            pst.setFloat(8, nadlt.getPeso());
			pst.setBoolean(9, nadlt.isBezerro());

            pst.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void update(AnimalNaoAdulto nadlt) {
		String query = "BEGIN;";
        query += "UPDATE animal SET raca = ?, peso = ? WHERE ano = ? AND mes = ? and num_brinco = ?;";
		query += "UPDATE naoadulto SET bezerro = ? WHERE ano = ? AND mes = ? and num_brinco = ?;";
        query += "COMMIT;";
		
		try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setString(1, nadlt.getRaca());
            pst.setFloat(2, nadlt.getPeso());
            pst.setInt(3, nadlt.getAno());
            pst.setInt(4, nadlt.getMes());
            pst.setInt(5, nadlt.getNumBrinco());
			
			pst.setBoolean(6, nadlt.isBezerro());
			pst.setInt(7, nadlt.getAno());
            pst.setInt(8, nadlt.getMes());
            pst.setInt(9, nadlt.getNumBrinco());

            pst.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

    public void save(AnimalNaoAdulto nadlt) {
        String query = "SELECT COUNT(*) FROM naoadulto WHERE ano = ? AND mes = ? AND num_brinco = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, nadlt.getAno());
            pst.setInt(2, nadlt.getMes());
            pst.setInt(3, nadlt.getNumBrinco());

            rs = pst.executeQuery();
            rs.next();

            if (rs.getInt("count") != 0) {
                update(nadlt);
            } else
                insert(nadlt);

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

    public void delete(AnimalNaoAdulto nadlt) {
        String query = "BEGIN;";
        query += "DELETE FROM animal WHERE ano = ? AND mes = ? AND num_brinco = ?;";
        query += "DELETE FROM naoadulto WHERE ano = ? AND mes = ? AND num_brinco = ?;";
        query += "COMMIT;";
        
        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, nadlt.getAno());
            pst.setInt(2, nadlt.getMes());
            pst.setInt(3, nadlt.getNumBrinco());
            pst.setInt(4, nadlt.getAno());
            pst.setInt(5, nadlt.getMes());
            pst.setInt(6, nadlt.getNumBrinco());

            pst.executeUpdate();

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }
}
